#!bin/picolisp lib.l
# 13oct06abu
# (c) Software Lab. Alexander Burger

# Must be run on a virtual console

(load "lib/misc.l" "lib/gcc.l")

### 'ioctl' glue function
(gcc "tst" NIL 'tone)

#include <sys/ioctl.h>
#include <linux/kd.h> // KIOCSOUND

// (tone 'freq) -> flg
any tone(any ex) {
   long amp = evCnt(ex,cdr(ex));
   long freq = evCnt(ex,cddr(ex));

   return ioctl(0, KIOCSOUND, amp==0 || freq==0? 0 : 1193180L/freq) < 0? Nil : T;
}

/**/

### Create named pipe
(unless (call 'test "-p" "fifo/tone")
   (call 'mkdir "-p" "fifo")
   (call 'mkfifo "fifo/tone") )

(push1 '*Bye '(call 'rm "fifo/tone"))


### Serve calls like:
#  (setq *Tone (open "fifo/tone"))
#  (out *Tone (pr 100 440))  # 440 Hz
#  (out *Tone (pr 0 0))    # Off
#  (close *Tone)
(loop
   (in "fifo/tone"
      (while (rd)
         (tone @ (rd)) ) ) )
